package com.ruoyi.system.service.impl;

import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.AffListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ADictIndustryAffiliationMapper;
import com.ruoyi.system.domain.ADictIndustryAffiliation;
import com.ruoyi.system.service.IADictIndustryAffiliationService;

/**
 * 分享类别Service业务层处理
 *
 * @author system
 * @date 2024-12-12
 */
@Service
public class ADictIndustryAffiliationServiceImpl implements IADictIndustryAffiliationService {
    @Autowired
    private ADictIndustryAffiliationMapper aDictIndustryAffiliationMapper;

    /**
     * 查询分享类别
     *
     * @param id 分享类别主键
     * @return 分享类别
     */
    @Override
    public ADictIndustryAffiliation selectADictIndustryAffiliationById(Long id) {
        return aDictIndustryAffiliationMapper.selectADictIndustryAffiliationById(id);
    }

    /**
     * 查询分享类别列表
     *
     * @param aDictIndustryAffiliation 分享类别
     * @return 分享类别
     */
    @Override
    public List<ADictIndustryAffiliation> selectADictIndustryAffiliationList(ADictIndustryAffiliation aDictIndustryAffiliation) {
        return aDictIndustryAffiliationMapper.selectADictIndustryAffiliationList(aDictIndustryAffiliation);
    }

    /**
     * 新增分享类别
     *
     * @param aDictIndustryAffiliation 分享类别
     * @return 结果
     */
    @Override
    public int insertADictIndustryAffiliation(ADictIndustryAffiliation aDictIndustryAffiliation) {
        aDictIndustryAffiliation.setCreateTime(DateUtils.getNowDate());
        boolean flag = !aDictIndustryAffiliation.getParentId().equals(0L);
        ADictIndustryAffiliation info = aDictIndustryAffiliationMapper.selectADictIndustryAffiliationById(aDictIndustryAffiliation.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (flag && info == null && !UserConstants.DEPT_NORMAL.equals(info.getDisFlag()))
        {
            throw new ServiceException("分类停用，不允许新增");
        }
        String ancestors = "";
        if(null != info){
            ancestors = info.getAncestors() + ",";
        }
        aDictIndustryAffiliation.setAncestors(ancestors + aDictIndustryAffiliation.getParentId());
        return aDictIndustryAffiliationMapper.insertADictIndustryAffiliation(aDictIndustryAffiliation);
    }

    /**
     * 修改分享类别
     *
     * @param aDictIndustryAffiliation 分享类别
     * @return 结果
     */
    @Override
    public int updateADictIndustryAffiliation(ADictIndustryAffiliation aDictIndustryAffiliation) {
        aDictIndustryAffiliation.setUpdateTime(DateUtils.getNowDate());
        ADictIndustryAffiliation newParentCategory = aDictIndustryAffiliationMapper.selectADictIndustryAffiliationById(aDictIndustryAffiliation.getParentId());
        ADictIndustryAffiliation oldCategory = aDictIndustryAffiliationMapper.selectADictIndustryAffiliationById(aDictIndustryAffiliation.getId());
        if (StringUtils.isNotNull(newParentCategory) && StringUtils.isNotNull(oldCategory))
        {
            String newAncestors = newParentCategory.getAncestors() + "," + newParentCategory.getId();
            String oldAncestors = oldCategory.getAncestors();
            aDictIndustryAffiliation.setAncestors(newAncestors);
            updateADictIndustryAffiliationChildren(aDictIndustryAffiliation.getId(), newAncestors, oldAncestors);
        }
        int result = aDictIndustryAffiliationMapper.updateADictIndustryAffiliation(aDictIndustryAffiliation);
        if (UserConstants.DEPT_NORMAL.equals(aDictIndustryAffiliation.getDisFlag()) && StringUtils.isNotEmpty(aDictIndustryAffiliation.getAncestors())
                && !StringUtils.equals("0", aDictIndustryAffiliation.getAncestors()))
        {
            // 如果该部门是启用状态，则启用该部门的所有上级部门
            updateParentCategoryStatusNormal(aDictIndustryAffiliation);
        }
        return result;
    }

    /**
     * 批量删除分享类别
     *
     * @param ids 需要删除的分享类别主键
     * @return 结果
     */
    @Override
    public int deleteADictIndustryAffiliationByIds(Long[] ids) {
        return aDictIndustryAffiliationMapper.deleteADictIndustryAffiliationByIds(ids);
    }

    /**
     * 删除分享类别信息
     *
     * @param id 分享类别主键
     * @return 结果
     */
    @Override
    public int deleteADictIndustryAffiliationById(Long id) {
        return aDictIndustryAffiliationMapper.deleteADictIndustryAffiliationById(id);
    }

    /**
     * 批量逻辑删除分享类别
     *
     * @param ids 需要删除的分享类别主键
     * @return 结果
     */
    @Override
    public int logicDeleteADictIndustryAffiliationByIds(Long[] ids) {
        return aDictIndustryAffiliationMapper.logicDeleteADictIndustryAffiliationByIds(ids);
    }
    /**
     * 逻辑删除分享类别信息
     *
     * @param id 分享类别主键
     * @return 结果
     */
    @Override
    public int logicDeleteADictIndustryAffiliationById(Long id) {
        return aDictIndustryAffiliationMapper.logicDeleteADictIndustryAffiliationById(id);
    }

    /**
     * 校验名称是否唯一
     *
     * @param aDictIndustryAffiliation 分类信息
     * @return 结果
     */
    @Override
    public boolean checkNameUnique(ADictIndustryAffiliation aDictIndustryAffiliation) {
        Long id = StringUtils.isNull(aDictIndustryAffiliation.getId()) ? -1L : aDictIndustryAffiliation.getId();
        ADictIndustryAffiliation info = aDictIndustryAffiliationMapper.checkNameUnique(aDictIndustryAffiliation.getInName(), aDictIndustryAffiliation.getParentId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 根据ID查询所有子分类（正常状态）
     *
     * @param categoryId 分类ID
     * @return 子分类数
     */
    @Override
    public int selectNormalChildrenCategoryById(Long categoryId) {
        return aDictIndustryAffiliationMapper.selectNormalChildrenCategoryById(categoryId);
    }


    /**
     * 修改该分类的父级分类状态
     *
     * @param aDictIndustryAffiliation 当前分类
     */
    private void updateParentCategoryStatusNormal(ADictIndustryAffiliation aDictIndustryAffiliation) {
        String ancestors = aDictIndustryAffiliation.getAncestors();
        Long[] ids = Convert.toLongArray(ancestors);
        aDictIndustryAffiliationMapper.updateCategoryStatusNormal(ids);
    }

    /**
     * 修改子元素关系
     *
     * @param categoryId 被修改的分类ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateADictIndustryAffiliationChildren(Long categoryId, String newAncestors, String oldAncestors)
    {
        List<ADictIndustryAffiliation> children = aDictIndustryAffiliationMapper.selectChildrenById(categoryId);
        for (ADictIndustryAffiliation child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            aDictIndustryAffiliationMapper.updateCategoryChildren(children);
        }
    }

    @Override
    public List<AffListVO> selectADictIndustryAffList() {
        List<AffListVO> vo = aDictIndustryAffiliationMapper.selectADictIndustryAffList();
        AffListVO v = new AffListVO();
        v.setText("不限");
        v.setValue("0");
        vo.set(0,v);
        return vo;
    }

    @Override
    public List<ADictIndustryAffiliation> selectADictIndustryAffiliationAllList() {
        return aDictIndustryAffiliationMapper.selectADictIndustryAffiliationAllList();
    }
}
